/**  
* @Title: ShopPrintService.java
* @Package com.lemon.service.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年11月14日
* @version V1.0  
*/
package com.lemon.service.cityRun;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.lemon.mapper.cityRun.CrShopOrderGoodsMapper;
import com.lemon.mapper.cityRun.CrShopOrdersMapper;
import com.lemon.mapper.cityRun.CrShopPrinterMapper;
import com.lemon.model.cityRun.CrShopOrderGoods;
import com.lemon.model.cityRun.CrShopOrders;
import com.lemon.model.cityRun.CrShopPrinter;
import com.lemon.utils.CityRunUtils;
import com.lemon.utils.StringUtil;
import com.lemon.utils.printer.PrinterUtils;

/**
 * <p>Title: ShopPrintService</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年11月14日
 * @版本 V 1.0 
 */
@Service
public class ShopPrintServiceImpl implements ShopPrintService {

	private static Logger log = LoggerFactory.getLogger(ShopPrintServiceImpl.class);

	@Resource
	private CrShopPrinterMapper pMapper;

	@Resource
	private CrShopOrdersMapper oMapper;

	@Resource
	private CrShopOrderGoodsMapper goMapper;

	/**
	 * <p>
	 * Title: selectPrintListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param printer
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月14日
	 */
	@Override
	public List<CrShopPrinter> selectPrintListBySelective(CrShopPrinter printer) throws Exception {
		PageHelper.startPage(printer.getPageNum() , printer.getPageSize());
		return pMapper.selectListBySelective(printer);
	}

	/**
	 * <p>
	 * Title: printerInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param shopI
	 * @param orderId
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月14日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public void printerInfo(String shopId , String orderId) throws Exception {

		CrShopOrders order = new CrShopOrders();
		order.setId(orderId);
		order = oMapper.selectByPrimaryKey(orderId);
		CrShopOrders or = new CrShopOrders();
		or.setShopId(shopId);
		or.setType(order.getType());
		or.setWhere(" and a.create_time >= ? " , CityRunUtils.getBusineDateString());
		/*
		 * int coun = oMapper.selectCountBySelective(or);
		 * order.setSort(coun);
		 */
		//查询订单的菜品
		CrShopOrderGoods ogo = new CrShopOrderGoods();
		ogo.setOrderId(orderId);
		List<CrShopOrderGoods> li = goMapper.selectListBySelective(ogo);
		order.setList(li);
		CrShopPrinter cp = new CrShopPrinter();
		cp.setShopId(shopId);
		if (StringUtil.isEmpty(shopId)) {
			System.out.println("当前shopid是null,可能是管理员账号,此时不打印数据");
			log.info("当前shopid是null,可能是管理员账号,此时不打印数据");
			return;
		}
		cp.setStatus(1);
		List<CrShopPrinter> listPrinters = pMapper.selectListBySelective(cp);

		if (listPrinters.size() <= 0) {
			return;
		}
		PrinterUtils.printer(listPrinters , order);
	}

	/**
	 * <p>
	 * Title: insertPrintSelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param printer
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月3日
	 */
	@Override
	public int insertPrintSelective(CrShopPrinter printer) throws Exception {
		return pMapper.insertSelective(printer);
	}

	/**
	 * <p>
	 * Title: updatePrintSelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param printer
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月3日
	 */
	@Override
	public int updatePrintSelective(CrShopPrinter printer) throws Exception {

		return pMapper.updateByPrimaryKeySelective(printer);
	}
}
